/**
 * Created by bdbs on 2017/6/21.
 */
export const type = {
  lrc: 'lrc',
  srt: 'srt',
};

function parseLrc(lrc) {
  // console.log('lrc', lrc);
  return lrc.split(/\n/).reduce((arr, centence) => {
    const centenceData = centence.match(/\[(\d\d):(\d\d).(\d\d)\]([\w\W]+)/);
    if (centenceData) {
      const time = (centenceData[1] * 60 * 10) + (centenceData[2] * 10) + (Math.round(centenceData[3] * 0.01) ? 10 : 5);
      arr.push({
        time,
        subtitle: [centenceData[4]],
      })
    }
    return arr;
  }, []);
}

// function parseSrt(srt) {
//   return srt.split(/[\n]\d+\r/g).map((centence) => {
//     const centenceData = centence.match(/\d?\n(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d[\n]([\w\W]+?)(\r[\w\W]+?)?($|\r)/);
//     const time = (centenceData[1] * 60 * 60 * 10) + (centenceData[2] * 60 * 10) + (centenceData[3] * 10) + (Math.round(centenceData[4] * 0.001) ? 10 : 5);
//     return {
//       time,
//       subtitle: [centenceData[5].replace(/[\n\r]/g, ''), centenceData[6] && centenceData[6].replace(/[\n\r]/g, '')],
//     };
//   });
// }

function parseSrt(srt) {
  // console.log('111', srt, srt.replace(/[\n\r]/g, 'HHXXHH').replace(/HHXXHH/g, 'FFZXJJJ'));
  // return srt.split(/\n\d+\n/g).map((centence) => {
  //   console.log(centence);
  //   // const centenceData = centence.match(/1?\n(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d[\r\n]([\w\W]+?)(\r[\w\W]+?)?($|\r)/);
  //   const centenceData = centence.match(/[\n\r]?(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d[\n\r]([\w\W]+?)([\r\n][\w\W]+?)?($|\r)/);
  //   const time = (centenceData[1] * 60 * 60 * 10) + (centenceData[2] * 60 * 10) + (centenceData[3] * 10) + (Math.round(centenceData[4] * 0.001) ? 10 : 5);
  //   return {
  //     time,
  //     subtitle: [centenceData[5].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,""), centenceData[6] && centenceData[6].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,"")],
  //   };
  // });
  // return srt.split(/\n\d+\n/g).reduce((centences, centence) => {
  //   // const centenceData = centence.match(/1?\n(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d[\r\n]([\w\W]+?)(\r[\w\W]+?)?($|\r)/);
  //   const centenceData = centence.match(/[\n\r]?(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d[\n\r]([\w\W]+?)([\r\n][\w\W]+?)?($|\r)/);
  //   console.log('centenceData', centenceData);
  //   if (!centenceData) return centences;
  //   console.log('centenceData', centence);
  //   const time = (centenceData[1] * 60 * 60 * 10) + (centenceData[2] * 60 * 10) + (centenceData[3] * 10) + (Math.round(centenceData[4] * 0.001) ? 10 : 5);
  //   const subtitle = (centenceData[5] + ' ' + (centenceData[6] ? centenceData[6] : '')).replace(/[\n\r]/g, '').replace(/<[^>]+>/g,"");
  //
  //   if (subtitle.length < 50 && centences.length && centences[centences.length - 1].subtitle.length < 100) {
  //     centences[centences.length - 1].subtitle += (' ' + subtitle);
  //   } else {
  //     centences.push({
  //       time,
  //       subtitle: subtitle,
  //     });
  //   }
  //
  //   // centences.push({
  //   //   time,
  //   //   subtitle: centenceData[5].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,""),
  //   //   subtitle2: centenceData[6] && centenceData[6].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,""),
  //   // });
  //
  //   return centences;
  // }, []);
  const lrcArr = srt.replace(/(\d\d):(\d\d):(\d\d),(\d\d\d) ?--> ?\d\d:\d\d:\d\d,\d\d\d/g, (...args) => {
    const time = (args[1] * 60 * 60 * 10) + (args[2] * 60 * 10) + (args[3] * 10) + (Math.round(args[4] * 0.001) ? 10 : 5);
    return `<==start==><==time${time}==>`;
  })
  const centences = lrcArr.split('<==start==>').reduce((centences, centence) => {
    centence = centence.replace(/\d+[\r\n]/, '');
    const centenceData = centence.match(/[\n\r]([\w\W]+?)([\r\n][\w\W]+?)?($|\r)/);
    const timeData = centence.match(/<==time(\d+)==>/);
    if (!centenceData || !timeData) return centences;
    // console.log('centenceData', timeData, centenceData);
    const time = timeData[1]
    const subtitle = (centenceData[1] + ' ' + (centenceData[2] ? centenceData[2] : '')).replace(/[\n\r]/g, '').replace(/<[^>]+>/g,"");

    if (subtitle.length < 50 && centences.length && centences[centences.length - 1].subtitle.length < 100) {
      centences[centences.length - 1].subtitle += (' ' + subtitle);
    } else {
      centences.push({
        time,
        subtitle: subtitle,
      });
    }

    // centences.push({
    //   time,
    //   subtitle: centenceData[5].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,""),
    //   subtitle2: centenceData[6] && centenceData[6].replace(/[\n\r]/g, '').replace(/<[^>]+>/g,""),
    // });

    return centences;
  }, []);

  console.log('centencescentences', centences);
  return centences;
}

export default function (data, fileType) {
  switch (fileType) {
    case type.lrc:
      return parseLrc(data);
      break;
    case type.srt:
      console.log(parseSrt(data));
      return parseSrt(data);
      break;
    default:
      return [];
  }
}
